package com.bikes.mileagetracker;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bikes.mileagetracker.entity.TrackingEntity;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {

    private List<TrackingEntity> trackingEntities;
    private Context mContext;
    private LayoutInflater inflater;

    public MyRecyclerAdapter(Context context, List<TrackingEntity> trackingEntities){
        this. mContext=context;
        this. trackingEntities=trackingEntities;
        inflater=LayoutInflater. from(mContext);
    }

    @Override
    public int getItemCount() {

        return trackingEntities.size();
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

        holder.vwStartDate.setText( trackingEntities.get(position).getStartDate());
        holder.vwEndDate.setText( trackingEntities.get(position).getEndDate());
        holder.vwMile.setText( Long.toString(trackingEntities.get(position).getMiles()));
        holder.vwStartTime.setText( trackingEntities.get(position).getStartTime());
        holder.vwEndTime.setText( trackingEntities.get(position).getEndTime());
        holder.vwStartFrom.setText( trackingEntities.get(position).getStartAddress());
        holder.vwEndTo.setText( trackingEntities.get(position).getEndAddress());
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_vw_root,parent, false);
        return new MyViewHolder(view);
    }

    class MyViewHolder extends ViewHolder{
        TextView vwStartDate;
        TextView vwEndDate;
        TextView vwMile;
        TextView vwStartTime;
        TextView vwEndTime;
        TextView vwStartFrom;
        TextView vwEndTo;
        MyViewHolder(View view) {
            super(view);
             vwStartDate = (TextView)view.findViewById(R.id.vw_start_date_text);
             vwEndDate = (TextView)view.findViewById(R.id.vw_end_date_text);
             vwMile = (TextView)view.findViewById(R.id.vw_mile_text);
             vwStartTime = (TextView)view.findViewById(R.id.vw_start_time_text);
             vwEndTime = (TextView)view.findViewById(R.id.vw_end_time_text);
             vwStartFrom = (TextView)view.findViewById(R.id.vw_start_from_text);

             vwEndTo = (TextView)view.findViewById(R.id.vw_end_to_text);

        }

    }
}